package com.yuchengtech.test.wordprc;

import java.util.LinkedList;
import java.util.List;

import com.wox.action.JsonData;
import com.wox.action.WoxAction;

public class DBAndBeanPropertiesConvert extends WoxAction {

	private String way; 
	private String dataBaseNames,beanNames;
	
	
	public String getWay() {
		return way;
	}


	public void setWay(String way) {
		this.way = way;
	}


	public String getDataBaseNames() {
		return dataBaseNames;
	}


	public void setDataBaseNames(String dataBaseNames) {
		this.dataBaseNames = dataBaseNames;
	}


	public String getBeanNames() {
		return beanNames;
	}


	public void setBeanNames(String beanNames) {
		this.beanNames = beanNames;
	}

	
	

	public String excute() throws Exception {
		//to beanNames
		JsonData data=getJsonData();
		
		if("1".equals(way))
		{
			if(dataBaseNames!=null)
			{
				String beanNames2=convertToBeanNames(dataBaseNames);
				data.setJsonStr(beanNames2);
				data.setState(true);
				data.setMessage("1");
			}
		}//to DBnames
		else if("2".equals(way))
		{
			if(beanNames!=null)
			{
				String dbNames=convertToDbNames(beanNames);
				data.setJsonStr(dbNames);
				data.setState(true);
				data.setMessage("2");
			}
		}else
		{
			data.setMessage("不支持的转换类型!");
		}
		
		return JSONBACK;
	}

	
	public String convertToBeanNames(String dbNames)
	{
		 String[] sr=dbNames.split("[_]");
		  StringBuilder builder=new StringBuilder();
		 for(int i=0;i<sr.length;i++)
		 {	
			 String str2=sr[i];
			 if(i!=0)
			 {
				 String c=str2.charAt(0)+"";
			     builder.append(c.toUpperCase()+str2.substring(1).toLowerCase());
			 }
			 else
				 builder.append(str2.toLowerCase());
		   }
		return builder.toString();
	}
	
	
	public String convertToDbNames(String dbNames)
	{
		 
		  StringBuilder builder=new StringBuilder();
		  List<String> list=splitByUppercase(dbNames);
		 
		  for (String string : list) {
			  builder.append(string.toUpperCase()).append("_");
		}
		   builder.deleteCharAt(builder.length()-1);
		return builder.toString();
	}
	
	public List<String> splitByUppercase(String src)
	{
		LinkedList<String> list=new LinkedList<String>();
		int lastInd=0;
		for(int i=1;i<src.length();i++)
		{
			char c=src.charAt(i);
			if(Character.isUpperCase(c))
			{
				list.add(src.substring(lastInd,i));
				lastInd=i;
			}
		}
		if(lastInd!=src.length())
		{
			list.add(src.substring(lastInd,src.length()));
		}
		return list;
	}
}
